package ui.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import tree.PageNode;
import tree.PageTree;
import ui.Main;
import util.Util;

/**
 * Sets the board size for the current page.
 * @author TKington
 *
 */
public class SetBoardSizeAction extends AbstractAction {
	/**
	 * Creates a new SetBoardSizeAction.
	 */
	public SetBoardSizeAction() {
		putValue(NAME, "BrdSize");
		putValue(SHORT_DESCRIPTION, "Set Board Size");
		//putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		putValue(SMALL_ICON, Util.getIcon("BoardSize16.gif")); //$NON-NLS-1$
	}
	
	public void actionPerformed(ActionEvent evt) {
		String s = JOptionPane.showInputDialog(Main.frame, "New size?");
		
		try {
			int size = Integer.parseInt(s);
			
			if(size < 9 || size > 19) {
				JOptionPane.showMessageDialog(Main.frame, "Invalid size");
				return;
			}

			PageTree tree = PageTree.instance();
			PageNode curPage = tree.getSelectedPage();
			curPage.setBoardSize(size);
			Main.frame.setDirty(true);
			Main.frame.gotoPage(curPage, false);
		}
		catch(NumberFormatException e) {
			JOptionPane.showMessageDialog(Main.frame, "Invalid size");
		}
	}
}